package com.huahang.fashionpress.service.impl;

import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.LoginException;
import com.huahang.fashionpress.form.FormUser;
import com.huahang.fashionpress.mapper.UserMapper;
import com.huahang.fashionpress.model.User;
import com.huahang.fashionpress.service.ILonginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : 王翰聪
 * @since : 2019/08/21
 * @version : 1.0
 */
@Service(value = "loginServiceImpl")
public class LoginServiceImpl implements ILonginService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int login(FormUser formUser) throws LoginException {
        //根据用户名从数据库中找到user对象
        User user = userMapper.selectByUsername(formUser.getUserUsername());
        //如果返回空用户，则抛出用户名错误异常
        if (user == null) {
            throw new LoginException(ExceptionEnum.USERNAME_ERROR);
        }
        //如果返回了一个用户则验证用户密码是否正确
        if (user == null) {
            throw new LoginException(ExceptionEnum.PASSWORD_ERROR);
        }
        if (!user.getUserPassword().equals(formUser.getUserPassword())) {
            throw new LoginException(ExceptionEnum.PASSWORD_ERROR);

            }
            //如果用户名和密码都正确，则返回用户id作为登录成功凭证
            return user.getUserId();
        }

    }



